package cn.zniot.so.application.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 数据工具类
 */
public class NumberTools {

	/**
	 * 转换为16进制字符串
	 * @param content:byte 内容
	 * @return: 转换后的字符串
	 */
	public static String conv2HexString(byte[] content) {
		int nlen = content.length;
		String strResult = "";
		for (int i = 0; i < nlen; i++) {
			int value = content[i] & 0x000000ff;
			String szValue = new String(Integer.toHexString(value));
			if (szValue.length() < 2) {
				strResult += "0";
			}
			strResult += szValue;
			strResult += " ";
			if ((i % 30) == 0 && (i != 0)) {
				//  strResult += "\r\n";
			}
		}
		return strResult;
	}

	/**
	 * 填充固定的数据内容
	 */
	public static int fillContentWithFixLen(byte[] bafter, String content,
			int len, int fill, int startindex) {
		return fillContentWithFixLen(bafter, content.getBytes(), len, fill, startindex);
	}

	public static int fillContentWithFixLen(byte[] bafter, byte[] content,
			int len, int fill, int startindex) {
		if (bafter == null || content == null) {
			return -1;
		}
		int length = content.length;

		if (length > len) {
			return -1;
		}

		for (int i = 0; i < length; i++) {
			int value = (content[i] & 0x000000ff) - 48;
			bafter[startindex + i] = (byte) value;
		}
		for (int i = 0; i < len - length; i++) {

			bafter[startindex + length + i] = (byte) 0;
		}
		return startindex + len;
	}

	/**
	 * 短整形到字节数组的转换
	 */
	public static int short2byteArray(short area, byte[] smscmd, int pos) {

		smscmd[pos++] = (byte) (0xff & area);
		smscmd[pos++] = (byte) ((0xff00 & area) >> 8);

		return pos;
	}

	/**
	 * 根据协议要把报文中除去包头包尾的所有字节中的7D，7E进行转码(7d->7d+00,7e->7d+01)
	 */
	public static int conversePacket(byte[] source, int sourcelen, byte[] dest) {
		int pos = 0; //0位是报头
		for (int i = 0; i < sourcelen; i++) {
			if (source[i] == 0x7D) {
				dest[pos++] = 0x7d;
				dest[pos++] = 0x00;
			} else if (source[i] == 0x7E) {
				dest[pos++] = 0x7d;
				dest[pos++] = 0x01;
			} else {
				dest[pos++] = source[i];
			}
		}

		return pos;
	}

	/**
	 * 根据协议要把报文中除去包头包尾的所有字节中的7D+00，7d+01进行转码(7d<-7d+00,7e<-7d+01)
	 */
	public static int unConversePacket(byte[] source, int sourcelen,
			byte[] dest, int startindex) {
		int pos = 0; //0位是报头
		for (int i = 0; i < sourcelen; i++) {
			if (source[i + startindex] == 0x7D
					&& source[i + 1 + startindex] == 0x00) {
				dest[pos++] = 0x7d;
				i++;
			} else if (source[i + startindex] == 0x7D
					&& source[i + 1 + startindex] == 0x01) {
				dest[pos++] = 0x7e;
				i++;
			} else {
				dest[pos++] = source[startindex + i];
			}
		}

		return pos;
	}

	/**
	 * 在指定时间上增加小时数
	 */
	public static String addHour(String start_date, int amount, String format)
			throws ParseException {
		Date date = parseStrTime(start_date, format);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, amount);
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(cal.getTime());
	}

	/**
	 * 解析指定的字符串时间
	 */
	public static Date parseStrTime(String time, String format)
			throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.parse(time);
	}

//	public static void main(String arg[]) {
//		/*try {
//			System.out.println(NumberTools.addHour("12-03-2510:05:46", 8, "yy-MM-ddHH:mm:ss"));
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}*/
//	}

}
